﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestFaxApp
{
    public class FaxService
    {
        private const String HOST = "25.45.160.184";
        private const String USERNAME = "mwulan";
        private const String PASSWORD = "apl1";
        private RFCOMAPILib.FaxServer server;


        public void TestConnection()
        {
            try
            {
                Console.WriteLine("Connecting....");

                server = new RFCOMAPILib.FaxServer();
                server.ServerName = HOST;
                server.Protocol = RFCOMAPILib.CommunicationProtocolType.cpTCPIP;
                server.UseNTAuthentication = RFCOMAPILib.BoolType.False;
                server.AuthorizationUserID = USERNAME;
                server.AuthorizationUserPassword = PASSWORD;

                server.OpenServer();
                String v_brooktrout_status_string;
                DateTime v_last_transmission_time;

                Console.WriteLine("Connected successfully");
                Console.WriteLine("Logged User : {0}", server.User.UserName);
                String filename = "RightFax" + DateTime.Now.ToShortDateString().Replace("/", "-") + " " + DateTime.Now.ToShortTimeString().Replace(":", ".");
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\" + filename + ".log"))
                {
                    Console.WriteLine("Retreiving fax list. Please wait . . .");
                    foreach (RFCOMAPILib.Fax fax in server.User.Faxes)
                    {
                        v_brooktrout_status_string = "";

                        if (fax.FaxID == 0) break;

                        if (fax.LastHistoryChangeDateTime < (new DateTime(1970, 1, 1, 0, 0, 0, 0)))
                        {
                            v_last_transmission_time = DateTime.Now;
                        }
                        else
                        {
                            v_last_transmission_time = fax.LastHistoryChangeDateTime;
                            if (fax.Histories.Count > 0)
                            {
                                RFCOMAPILib.FaxHistory rfcom_FaxHistory = (RFCOMAPILib.FaxHistory)fax.Histories[fax.Histories.Count];
                                if (rfcom_FaxHistory.TypeOfHistory == RFCOMAPILib.FaxHistoryType.FaxHistoryTransmission)
                                {
                                    RFCOMAPILib.TransmissionFaxHistory rfcom_FaxTransmision = (RFCOMAPILib.TransmissionFaxHistory)fax.Histories[fax.Histories.Count];
                                    v_brooktrout_status_string = "(" + rfcom_FaxTransmision.BrooktroutCallResultLineStatus.ToString() + @"\" + rfcom_FaxTransmision.BrooktroutCallResultStatus.ToString() + ":" + rfcom_FaxTransmision.BrooktroutFaxResultLineStatus.ToString() + @"\" + rfcom_FaxTransmision.BrooktroutFaxResultStatus.ToString() + ")";

                                    Console.WriteLine(v_brooktrout_status_string);
                                    file.WriteLine(v_brooktrout_status_string);
                                }
                            }
                        }
                    }
                    Console.WriteLine("Fax list retreived!");
                }

                //Console.WriteLine("Faxes : {0}", server.User.Faxes.Count);


                //foreach (RFCOMAPILib.User user in server.Users)
                //{
                //    Console.WriteLine("User {0} ", user.UserName);
                //    //Console.WriteLine("XML : {0}", user.Faxes.XML);
                //    if (user.UserName == "MWULAN")
                //    {
                //        //AuthUser = server.AuthorizationUser;
                //        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\" + filename + ".log"))
                //        {
                //            Console.WriteLine("Retreiving fax list. Please wait . . .");
                //            file.WriteLine("Fax Objects : ");
                //            file.WriteLine("Unique Id - Id. Filename | Sent Time | Fax Number | Fax Name");
                //            int count = 0;
                //            foreach (RFCOMAPILib.Fax fax in user.Faxes)
                //            {
                //                file.WriteLine("{0} - {1}. {2}", fax.UniqueID, fax.FaxID, fax.FaxFilename, fax.SendTime, fax.FromFaxNumber, fax.FromName);
                //                Console.WriteLine("Fax Retreived : {0}", ++count);
                //            }
                //            Console.WriteLine("All faxes of this user retreived! Total: " + count);
                //        }



                //    }
                //}
                Console.WriteLine("Closing . . .");
            }
            catch (Exception e)
            {

                Console.WriteLine("Error : " + e);
            }
            finally
            {
                server.CloseServer();
                Console.ReadKey();
            }

        }
    }
}
